package com.weigang.freemarker;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@Controller
public class UserController {
    @GetMapping("/users")
    public ModelAndView getUsers() {
        ModelAndView mv = new ModelAndView();
        List<User> userList = new ArrayList<>();
        userList.add(new User(1, "张三", "张三","admin"));
        userList.add(new User(2, "莉丝", "莉丝","super"));
        userList.add(new User(3, "GG", "GG","super"));
        List<String> roles = new ArrayList<>();
        roles.add("admin");
        roles.add("super");
//        model.addAttribute("users", userList);
//        model.addAttribute("roles", roles);

        mv.addObject("users", userList);
        mv.addObject("roles", roles);
        mv.setViewName("users");
        return mv;
    }


    public static class User{
        private Integer id;
        private String name;
        protected String author;

        private String role;

        public User(Integer id, String name, String author) {
            this.id = id;
            this.name = name;
            this.author = author;
        }

        public User() {
        }

        public User(Integer id, String name, String author, String role) {
            this.id = id;
            this.name = name;
            this.author = author;
            this.role = role;
        }


        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getAuthor() {
            return author;
        }

        public void setAuthor(String author) {
            this.author = author;
        }

        @Override
        public String toString() {
            return "User{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", author=" + author +
                    '}';
        }


        public String getRole() {
            return role;
        }

        public void setRole(String role) {
            this.role = role;
        }
    }
}

